//
//  HealthViewController.m
//  muscleLife
//
//  Created by xalo on 16/5/3.
//  Copyright © 2016年 HeavenAndHell. All rights reserved.
//

#import "HealthViewController.h"
#import <MobAPI/MobAPI.h>
#import "HealthSearchModel.h"
#import "HealthDetailViewController.h"
#import "SearchResultsViewController.h"
@interface HealthViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating,UISearchControllerDelegate,UISearchBarDelegate,SearchResultsViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,retain)UISearchController *searchController;
@property(nonatomic,retain)NSMutableArray *dataSourceArray;
@property(nonatomic, retain)NSMutableString* searchStr;
@property(nonatomic,retain)HealthSearchModel *healthSearchModel;
@end

@implementation HealthViewController
//数据源的懒加载
-(NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray=[NSMutableArray array];
    }
    return _dataSourceArray;
}
-(UISearchController *)searchController{
    if (!_searchController) {
        SearchResultsViewController *resultsVC=[[UIStoryboard storyboardWithName:@"Heath" bundle:nil] instantiateViewControllerWithIdentifier:@"SearchResultsViewController"];
        resultsVC.delegate = self;
        resultsVC.block = ^(HealthSearchModel* model){
            HealthDetailViewController *detailVC=[[UIStoryboard storyboardWithName:@"Heath" bundle:nil]instantiateViewControllerWithIdentifier:@"HealthDetailViewController"];
            detailVC.model=model;
            [self.navigationController pushViewController:detailVC animated:YES];
        };
        _searchController=[[UISearchController alloc]initWithSearchResultsController:resultsVC];
        [self.searchController.searchBar sizeToFit];//设置searchBar位置自适应
        self.searchController.searchBar.placeholder=@"请输入搜索内容";
        self.searchController.delegate=self;
//        self.tableView.tableHeaderView=self.searchController.searchBar;
        self.searchController.searchResultsUpdater=self;//设置显示搜索结果的控制器
        self.searchController.searchBar.delegate = self;
    }
    return _searchController;
}

#pragma mark---视图生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    self.automaticallyAdjustsScrollViewInsets=NO;
    [self searchController];
    self.tableView.tableHeaderView=self.searchController.searchBar;
    [self randomArrsearch];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//产生随机数组；
-(void)randomArrsearch{
//    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    NSArray* array = @[@"2",@"4",@"6",@"8",@"10",@"12",@"健康",@"绿色"];
    NSString* str = [array objectAtIndex:arc4random()%8];
    [self acquireSourceDataWithKeyWord:str page:@"1" size:@"10" finished:^(MOBAResponse *response) {
        if (response.error) {

        }else{
            NSArray* listArray = response.responder[@"list"];
            for (NSDictionary *itemDic in listArray) {
                HealthSearchModel *model=[[HealthSearchModel alloc]init];
                [model setValuesForKeysWithDictionary:itemDic];
                [self.dataSourceArray addObject:model];
            }
        }
//        [MBProgressHUD hideHUDForView:self.tableView animated:YES];
        [self.tableView reloadData];
    }];
}




#pragma mark----UISearchController delegate
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
   
    NSString *text=searchController.searchBar.text;
    if ([text length]!=0) {
       self.searchStr = [text mutableCopy];
        SearchResultsViewController *searchResultsVC = (SearchResultsViewController *)self.searchController.searchResultsController;
        searchResultsVC.searchStr = self.searchStr;
//        [searchResultsVC searchDatawithkey:self.searchStr];
        [searchResultsVC.tableView reloadData];
    }
}

- (void)didDismissSearchController:(UISearchController *)searchController{

    SearchResultsViewController *searchResultsVC = (SearchResultsViewController *)self.searchController.searchResultsController;
    [searchResultsVC.sourceArray removeAllObjects];    
    self.searchStr = nil;
}

-(void)willPresentSearchController:(UISearchController *)searchController{
    
    self.searchStr = [NSMutableString string];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;{

    SearchResultsViewController *searchResultsVC = (SearchResultsViewController *)self.searchController.searchResultsController;
    searchResultsVC.searchStr = self.searchStr;
    [searchResultsVC searchDatawithkey:self.searchStr];
    [searchResultsVC.tableView reloadData];
}

#pragma mark----UITableView的代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    self.healthSearchModel=self.dataSourceArray[indexPath.row];
    cell.textLabel.text=self.healthSearchModel.title;
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HealthDetailViewController *detailVC=[[UIStoryboard storyboardWithName:@"Heath" bundle:nil]instantiateViewControllerWithIdentifier:@"HealthDetailViewController"];
    detailVC.model=[self.dataSourceArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}

-(void)searchResultsViewController:(SearchResultsViewController *)searchResultsViewController sourceData:(NSArray *)array{

    [self.dataSourceArray removeAllObjects];
    [self.dataSourceArray addObjectsFromArray:array];
    [self.tableView reloadData];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
